package by.exposit.dao;

import by.exposit.dao.entity.*;
import by.exposit.domain.Project;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Repository
@Transactional
public class ProjectDAOImpl extends GenericDAOImpl<ProjectEntity,Long> implements ProjectDAO {



    @Override
    public List<ProjectEntity> projectUserList(UserEntity user) {

        List<RequestEntity> requests = null;
        Criteria cr = getSession()
                .createCriteria(ProjectEntity.class, "Project")
                .add(Restrictions.eq("user", user));
        return cr.list();
    }


    @Override
    public List<ProjectEntity> projectFieldList(FieldEntity field) {

    List<RequestEntity> requests = null;
        Criteria cr = getSession()
                .createCriteria(ProjectEntity.class, "Project")
                .add(Restrictions.eq("field", field));
        return cr.list();

    }




    @Override
    public List<ProjectEntity> projectUserWantToFinanceList(long userId) {

        return null;
    }




    @Override
    public List<ProjectEntity> priorityProjects() {


        List<ProjectEntity> templistproject = getAll();
        List<ProjectEntity> listproject = new ArrayList<ProjectEntity>();
        for(ProjectEntity p: templistproject)
        {
            Criteria cr = getSession()
                    .createCriteria(DonateEntity.class, "Donate" )
                    .add(Restrictions.eq("pocket", p.getPocket()));

            List<PocketEntity> pockets = cr.list();
            if(pockets.size() >= 3)
            {
                listproject.add(p) ;
            }
        }
        return listproject;
    }

    @Override
    public void changeState(Project project, float state)
    {
        Criteria cr = getSession().createCriteria(ProjectEntity.class, "Project" )
                .add(Restrictions.eq("project", project));
            ProjectEntity projectEntity = (ProjectEntity) cr.uniqueResult();
        projectEntity.setState(state);

          update(projectEntity);

    }

    @Override
    public void isWantToHelp(long projectId)
    {

    }
    @Override
    public List<FieldEntity> getFieldsInUsersProjects(UserEntity userEntity) {
        Criteria cr = getSession().createCriteria(ProjectEntity.class, "Project")
                .add(Restrictions.eq("user", userEntity));
        List<FieldEntity> fields = new ArrayList<FieldEntity>();
          List<ProjectEntity> projectEntities =     cr.list();
        int counter = 0;

            for(ProjectEntity projectEntity:projectEntities)
                 {
                     FieldEntity projectField = projectEntity.getField();
                     System.out.println("LoL");
                     for(FieldEntity field:fields)
                     {
                              if(field.equals(projectField) )
                              {
                                counter++;
                              }


                     }
                     if(counter == 0)
                     {
                         fields.add(projectField);
                     }
                 }
        return fields;

    }
}